package com.futures.investm.common;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.futures.investm.MainActivity;
import com.futures.investm.R;
import com.futures.investm.event.MineEvent;
import com.futures.investm.utils.CacheData;
import com.futures.investm.utils.IntentUtils;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.vise.xsnow.event.BusManager;

import butterknife.BindView;
import butterknife.OnClick;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.manager.LoggerManager;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.RegUtils;
import tech.com.commoncore.utils.ToastUtil;

public class LoginActivity extends BaseTitleActivity {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.forget_pw)
    TextView forgetPw;
    @BindView(R.id.regist)
    TextView regist;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("登陆")
                .setLeftTextDrawable(null)
                .setStatusBarLightMode(false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }


    @OnClick({R.id.forget_pw, R.id.regist, R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_pw:
                //忘记密码
                IntentUtils.goForgetPw(mContext);
                break;
            case R.id.regist:
                //注册
                IntentUtils.goRegister(mContext);
                break;
            case R.id.btn_login:
                //登陆
                login(etPhone.getText().toString().trim(), etPassword.getText().toString().trim());
                break;
        }
    }

    private void login(final String phone, final String password) {
        //手机号码空校验
        if (DataUtils.isEmptyString(phone)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        //手机号有效性校验
        if (!RegUtils.isMobile(phone)) {
            ToastUtil.show("请输入有效手机号");
            return;
        }

        //密码空校验
        if (DataUtils.isEmptyString(password)) {
            ToastUtil.show("请输入密码");
            return;
        }

        AVUser.loginByMobilePhoneNumberInBackground(phone, password, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (null == e) {
                    //登录成功
                    ToastUtil.show("登录成功");
                    //缓存登录信息
                    CacheData.setLoginUserId(mContext, avUser.getObjectId());
                    CacheData.setLoginUser(avUser);
                    CacheData.initLoginAccount(mContext, phone, password);
                    BusManager.getBus().post(new MineEvent());
                    FastUtil.startActivity(mContext, MainActivity.class);
                    LoginActivity.this.finish();
                } else {
                    //登录失败
                    LoggerManager.e(e.getMessage());
                    ToastUtil.show("账户或密码有误,登录失败");
                    CacheData.setLoginPassword(mContext, "");
                    etPassword.setText("");
                }
            }
        });
    }

}
